package com.dobbinsoft.iotechn.device.service.open;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.iotechn.core.constant.Const;
import com.dobbinsoft.iotechn.core.exception.ServiceException;
import com.dobbinsoft.iotechn.device.api.open.ControllerService;
import com.dobbinsoft.iotechn.device.domain.ProductControllerDO;
import com.dobbinsoft.iotechn.device.dto.ControllerComponentAttrDTO;
import com.dobbinsoft.iotechn.device.dto.ControllerComponentDTO;
import com.dobbinsoft.iotechn.device.mapper.ControllerComponentAttrMapper;
import com.dobbinsoft.iotechn.device.mapper.ControllerComponentMapper;
import com.dobbinsoft.iotechn.device.mapper.ProductControllerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 * User: rize
 * Date: 2020/6/23
 * Time: 15:54
 */
@Service("controllerService")
public class ControllerServiceImpl implements ControllerService {

    @Autowired
    private ProductControllerMapper productControllerMapper;

    @Autowired
    private ControllerComponentMapper controllerComponentMapper;

    @Autowired
    private ControllerComponentAttrMapper controllerComponentAttrMapper;

    @Override
    @DS(Const.DB_S)
    public List<ProductControllerDO> list(Long productId) throws ServiceException {
        return productControllerMapper.selectList(new QueryWrapper<ProductControllerDO>().eq("product_id", productId));
    }

    @Override
    @DS(Const.DB_S)
    public List<ControllerComponentDTO> listComponent(Long controllerId) throws ServiceException {
        List<ControllerComponentDTO> controllerComponentList = controllerComponentMapper.getControllerComponent(controllerId);
        controllerComponentList.forEach(item -> {
            List<ControllerComponentAttrDTO> componentAttrList = controllerComponentAttrMapper.getComponentAttrList(item.getId());
            item.setAttrList(componentAttrList);
        });
        return controllerComponentList;
    }

}
